package com.bjpowernode.crm.workbench.web.controller;

import com.bjpowernode.crm.commons.contants.Contants;
import com.bjpowernode.crm.commons.domain.ReturnObject;
import com.bjpowernode.crm.commons.utils.DateUtils;
import com.bjpowernode.crm.commons.utils.UUIDUtils;
import com.bjpowernode.crm.settings.domain.User;
import com.bjpowernode.crm.settings.service.DicTypeService;
import com.bjpowernode.crm.settings.service.DicValueService;
import com.bjpowernode.crm.settings.service.UserService;
import com.bjpowernode.crm.workbench.domain.Activity;
import com.bjpowernode.crm.workbench.domain.ActivityRemark;
import com.bjpowernode.crm.workbench.domain.Clue;
import com.bjpowernode.crm.workbench.domain.ClueRemark;
import com.bjpowernode.crm.workbench.service.ActivityService;
import com.bjpowernode.crm.workbench.service.ClueRemarkService;
import com.bjpowernode.crm.workbench.service.ClueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.management.counter.perf.PerfInstrumentation;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @ClassName : ClueRemarkController
 * @Author : 司猫子
 * @Date: 2021/1/9 15:28
 */
@Controller
public class ClueRemarkController {
    @Autowired
    private ClueService clueService;
    @Autowired
    private UserService userService;
    @Autowired
    private ActivityService activityService;
    @Autowired
    private DicTypeService dicTypeService;
    @Autowired
    private DicValueService dicValueService;
    @Autowired
    private ClueRemarkService clueRemarkService;

    @RequestMapping("/workbench/clue/detailClue.do")
    public String toDetailClue(String id, Model model, HttpSession session){
        Clue clue = clueService.queryClueDetailById(id);
        List<ClueRemark> clueRemarks = clueRemarkService.selectClueRemarkForDetailByClueId(id);
        List<Activity> activityList = activityService.selectActivityForDetailByClueId(id);
        model.addAttribute("activityList",activityList);
        model.addAttribute("clue",clue);
        model.addAttribute("remarkList",clueRemarks);
        return "workbench/clue/detail";
    }

    @RequestMapping("/workbench/clue/saveCreateClueRemark.do")
    @ResponseBody
    public Object addClueRemark(ClueRemark clueRemark, HttpSession session){
        User user = (User)session.getAttribute(Contants.SUSSION_USER);
        ReturnObject returnObject = new ReturnObject();
        clueRemark.setId(UUIDUtils.getUUID());
        clueRemark.setCreateTime(DateUtils.formatDateTime(new Date()));
        clueRemark.setCreateBy(user.getId());
        clueRemark.setEditFlag("0");
        int i = clueRemarkService.insert(clueRemark);
        if(i == 0){
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("添加备注失败,请稍后重试");
        }else {
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
            returnObject.setRetData(clueRemark);
        }
        return returnObject;
    }

    @RequestMapping("/workbench/clue/saveEditClueRemark.do")
    @ResponseBody
    public Object saveEditActivityRemark(String id,String noteContent,HttpSession session){
        User user = (User)session.getAttribute(Contants.SUSSION_USER);
        ReturnObject returnObject = new ReturnObject();
        ClueRemark clueRemark = clueRemarkService.selectByPrimaryKey(id);
        clueRemark.setEditTime(DateUtils.formatDateTime(new Date()));
        clueRemark.setNoteContent(noteContent);
        clueRemark.setEditBy(user.getId());
        clueRemark.setEditFlag("1");
        int i = clueRemarkService.updateByPrimaryKey(clueRemark);
        if(i == 1){
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
            returnObject.setRetData(clueRemark);
        }else {
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("更新失败,请稍后重试");
        }

        return returnObject;
    }


}
